﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ScheduleSystem_Tasks_NewTask : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ListBoxDataBind();
        }
    }
    protected void ListBoxDataBind()
    {
        //Get the list of all employees
        UserName.DataSource = Roles.GetUsersInRole("Employee"); 
        UserName.DataBind();
        //Get the list of all customers
        Customer.DataSource = Roles.GetUsersInRole("Customer");
        Customer.DataBind();
    }
    protected void TaskName_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Customer_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Save_Click(object sender, EventArgs e)
    {
        //MembershipUser newUser = Membership.GetUser();


        Guid newTaskId = Guid.NewGuid();
        String Title = TaskName.Text;
        DateTime Start_Date = Convert.ToDateTime(StartDate.Text);
        DateTime Due_Date = Convert.ToDateTime(DueDate.Text);
        int Complete_P = Convert.ToInt32(Complete.Text);
        String Description_T = Description.Text;
        int Priority_T = Convert.ToInt32(Priority.Text);
        String Status_T = Status.SelectedValue;
        DateTime AssignmentDate = Convert.ToDateTime(Assignment_date.Text);
        DateTime Creation_Date = DateTime.Now;
        Guid UserGuid= (Guid)Membership.GetUser(UserName.SelectedValue).ProviderUserKey;
        Guid CustomerGuid = (Guid)Membership.GetUser(Customer.SelectedValue).ProviderUserKey;
        
        // Get your Connection String from the web.config. 

        string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        string insertSql = "INSERT INTO Task  VALUES(@newTaskId, @Title,@Start_Date,@Due_Date,@Complete_P,@Description_T,@Priority_T,@Status_T,@AssignmentDate,@Creation_Date,@UserGuid,@CustomerGuid)";

        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {

            myConnection.Open();
            SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
            myCommand.Parameters.AddWithValue("@newTaskId", newTaskId);
            myCommand.Parameters.AddWithValue("@Title", Title);
            myCommand.Parameters.AddWithValue("@Start_Date", Start_Date);
            myCommand.Parameters.AddWithValue("@Due_Date", Due_Date);
            myCommand.Parameters.AddWithValue("@Complete_P", Complete_P);
            myCommand.Parameters.AddWithValue("@Description_T", Description_T);
            myCommand.Parameters.AddWithValue("@Priority_T", Priority_T);
            myCommand.Parameters.AddWithValue("@Status_T", Status_T);
            myCommand.Parameters.AddWithValue("@AssignmentDate", AssignmentDate);
            myCommand.Parameters.AddWithValue("@Creation_Date", Creation_Date);
            myCommand.Parameters.AddWithValue("@UserGuid", UserGuid);
            myCommand.Parameters.AddWithValue("@CustomerGuid", CustomerGuid);
            myCommand.ExecuteNonQuery();

            myConnection.Close();

        }

    }
}